//
//  LKBaseView.m
//  YLHelper
//
//  Created by she1323 on 2018/4/10.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKBaseView.h"

@implementation LKBaseLabel

+(instancetype)labelWithFrame:(CGRect)frame text:(NSString *)text textColor:(UIColor *)textColor font:(UIFont *)font textAlignment:(NSTextAlignment)textAlignment backgroundColor:(UIColor *)bgColor{
    LKBaseLabel *label = [[LKBaseLabel alloc] initWithFrame:frame];
    label.text = text;
    label.textColor = textColor;
    label.font = font;
    label.textAlignment = textAlignment;
    label.backgroundColor = bgColor;
    return label;
}

@end

@implementation LKBaseButton

+(instancetype)buttonWithFrame:(CGRect)frame type:(UIButtonType)type title:(NSString *)title titleColor:(UIColor *)color layercorne:(CGFloat)radio font:(UIFont *)font backgroudColor:(UIColor *)bgcolor AndActionBlock:(actionBlock)block{
    LKBaseButton *button = [LKBaseButton buttonWithType:type];
    button.frame = frame;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateNormal];
    button.layer.cornerRadius = radio;
    button.layer.masksToBounds = YES;
    button.backgroundColor = bgcolor;
    button.titleLabel.font = font;
    [button addTarget:button action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    button.block = block;
    return button;

}
-(void)buttonAction:(UIButton *)btn{
    WS(weakSelf)
    if (weakSelf.block) {
        __strong typeof(self) strongSelf = weakSelf;
        strongSelf.block(btn);
    }
}


@end

@implementation TimeBtn
{
    UIActivityIndicatorView * activityV;
}


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self)
    {
        self.titleLabel.font = [UIFont systemFontOfSize:12];
        [self.titleLabel setTextColor:[UIColor blackColor]];
        self.titleLabel.minimumScaleFactor=12;
        self.layer.masksToBounds=YES;
        self.layer.cornerRadius=5;
        [self addTarget:self
                 action:@selector(eventForClick)
       forControlEvents:UIControlEventTouchUpInside];
        [self setupInitialState];
    }
    return self;
}



-(void)initActivityView
{
    activityV  = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityV.frame=CGRectMake(0, 0, self.frame.size.height, self.frame.size.height);
    activityV.hidden=YES;
    [self addSubview:activityV];
    
}

- (void)startAnimating
{
    [UIView animateWithDuration:0.3 animations:^{
        activityV.hidden =YES;
    }];
    
    [self setTitle:@"获取中" forState:UIControlStateNormal];
    [activityV startAnimating];
    self.userInteractionEnabled = NO;
}

- (void)stopAnimating
{
    //[UIView animateWithDuration:0.3 animations:^{
    activityV.hidden=NO;
    //}];
    
    [activityV stopAnimating];
}

- (void)setupInitialState {
    [self setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.userInteractionEnabled = YES;
}

- (void)setupFailState {
    [self stopAnimating];
    [self setTitle:@"重获验证码" forState:UIControlStateNormal];
    [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.userInteractionEnabled = YES;
}

- (void)setupRunningState:(NSString *)timerSec {
    NSString *title = [NSString stringWithFormat:@"%@秒",timerSec];
    [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self setTitle:title forState:UIControlStateNormal];
}

- (void)eventForClick {
    [self initActivityView];
    
    if (self.clickblock) {
        self.clickblock();
    }
}

- (void)startTimer {
    [self stopAnimating];
    self.userInteractionEnabled = NO;
    __block int timeout=60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,DISPATCH_TIME_NOW,1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0)
        { //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self setupInitialState];
                
                
            });
        }
        else
        {
            NSString *strTime = [NSString stringWithFormat:@"%.2d", timeout];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self setupRunningState:strTime];
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

@end




@implementation LKBaseTableview

+(instancetype)setTableviewWithFrame:(CGRect)frame style:(UITableViewStyle)style registercellClasss:(NSArray *)classnames{
    
    LKBaseTableview *tableview = [[LKBaseTableview alloc] initWithFrame:frame style:style];
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableview.showsVerticalScrollIndicator = NO;
    tableview.showsHorizontalScrollIndicator = NO;
    if (classnames != nil) {
        for (NSString *classname in classnames) {
            UINib *nib = [UINib nibWithNibName:classname bundle:nil];
            [tableview registerNib:nib forCellReuseIdentifier:classname];
        }
    }

    return tableview;
}

@end

@implementation LKBaseLineView

+(instancetype)setLineView{
    LKBaseLineView *view = [[LKBaseLineView alloc] init];
    view.backgroundColor = LineColor;
    return view;
}
@end

@implementation LKBaseCell

-(void)awakeFromNib{
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}
@end
